package com.xianggu.threadPool.rejectStrategy;

import com.xianggu.threadPool.MyThreadPool;

import java.util.concurrent.BlockingQueue;

/**
 * @author chengjy
 * @date 2025/3/14
 */
public class DiscardRejectHandle implements RejectHandle {
    /**
     * 拒绝策略：先删除一个已有的任务，再添加新的任务
     * @param myThreadPool
     * @param runnable
     */
    @Override
    public void reject(MyThreadPool myThreadPool, Runnable runnable) {
        BlockingQueue<Runnable> taskQueue = myThreadPool.getTaskQueue();
        Runnable remove = taskQueue.remove();// 先删除一个任务
        System.out.println("当前线程池已满，删除一个任务，添加新的任务");
        taskQueue.offer(runnable);
    }
}
